package com.eian.boot.module.system.permission.service;

import com.eian.boot.common.constants.CacheConstants;
import com.eian.boot.redis.RedisHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 权限缓存服务
 *
 * @author alex.meng
 */
@Slf4j
@Service
@AllArgsConstructor
public class PermissionCacheService {
    private final RedisHelper redisHelper;

    /**
     * @author alex.meng
     * @description 获取用户权限缓存
     **/
    public List<String> getUserPermissions(Long userId) {
        String key = CacheConstants.Keys.USER_PERMS + userId;
        return redisHelper.getFromList(key);
    }

    /**
     * @author alex.meng
     * @description 设置用户权限缓存
     **/
    public void setUserPermissions(Long userId, List<String> permissions) {
        String key = CacheConstants.Keys.USER_PERMS + userId;
        redisHelper.addToList(key, permissions,
                CacheConstants.Expire.USER_PERMS_TIME,
                CacheConstants.Expire.USER_PERMS_UNIT);
        log.debug("缓存用户权限: userId={}, count={}", userId, permissions.size());
    }

    /**
     * @author alex.meng
     * @description 清除用户权限缓存
     **/
    public void clearUserPermissions(Long userId) {
        String key = CacheConstants.Keys.USER_PERMS + userId;
        redisHelper.remove(key);
        log.debug("清除用户权限缓存: userId={}", userId);
    }

    /**
     * @author alex.meng
     * @description 获取用户角色缓存
     **/
    public List<String> getUserRoles(Long userId) {
        String key = CacheConstants.Keys.USER_ROLES + userId;
        return redisHelper.getFromList(key);
    }

    /**
     * @author alex.meng
     * @description 设置用户角色缓存
     **/
    public void setUserRoles(Long userId, List<String> roles) {
        String key = CacheConstants.Keys.USER_ROLES + userId;
        redisHelper.addToList(key, roles,
                CacheConstants.Expire.USER_ROLES_TIME,
                CacheConstants.Expire.USER_ROLES_UNIT);
        log.debug("缓存用户角色: userId={}, count={}", userId, roles.size());
    }

    /**
     * @author alex.meng
     * @description 清除用户角色缓存
     **/
    public void clearUserRoles(Long userId) {
        String key = CacheConstants.Keys.USER_ROLES + userId;
        redisHelper.remove(key);
        log.debug("清除用户角色缓存: userId={}", userId);
    }

    /**
     * @author alex.meng
     * @description 获取用户菜单缓存
     **/
    public <T> T getUserMenus(Long userId) {
        String key = CacheConstants.Keys.USER_MENUS + userId;
        return redisHelper.get(key);
    }

    /**
     * @author alex.meng
     * @description 设置用户菜单缓存
     **/
    public <T> void setUserMenus(Long userId, T menus) {
        String key = CacheConstants.Keys.USER_MENUS + userId;
        redisHelper.set(key, menus,
                CacheConstants.Expire.USER_MENUS_TIME,
                CacheConstants.Expire.USER_MENUS_UNIT);
        log.debug("缓存用户菜单: userId={}", userId);
    }

    /**
     * @author alex.meng
     * @description 清除用户菜单缓存
     **/
    public void clearUserMenus(Long userId) {
        String key = CacheConstants.Keys.USER_MENUS + userId;
        redisHelper.remove(key);
        log.debug("清除用户菜单缓存: userId={}", userId);
    }

    /**
     * @author alex.meng
     * @description 获取角色权限缓存
     **/
    public Set<Long> getRolePermissions(Long roleId) {
        String key = CacheConstants.Keys.ROLE_PERMS + roleId;
        return redisHelper.getFromSet(key);
    }

    /**
     * @author alex.meng
     * @description 设置角色权限缓存
     **/
    public void setRolePermissions(Long roleId, List<Long> permissions) {
        String key = CacheConstants.Keys.ROLE_PERMS + roleId;
        redisHelper.addToSet(key, permissions,
                CacheConstants.Expire.ROLE_PERMS_TIME,
                CacheConstants.Expire.ROLE_PERMS_UNIT);
        log.debug("缓存角色权限: roleId={}, count={}", roleId, permissions.size());
    }

    /**
     * @author alex.meng
     * @description 清除角色权限缓存
     **/
    public void clearRolePermissions(Long roleId) {
        String key = CacheConstants.Keys.ROLE_PERMS + roleId;
        redisHelper.remove(key);
        log.debug("清除角色权限缓存: roleId={}", roleId);
    }

    /**
     * @author alex.meng
     * @description 清除用户所有缓存
     **/
    public void clearUserAllCache(Long userId) {
        clearUserPermissions(userId);
        clearUserRoles(userId);
        clearUserMenus(userId);
        log.info("清除用户所有缓存: userId={}", userId);
    }

    /**
     * @author alex.meng
     * @description 批量清除用户所有缓存
     **/
    public void clearUserAllCacheBatch(List<Long> userIds) {
        userIds.forEach(this::clearUserAllCache);
        log.info("批量清除用户缓存: count={}", userIds.size());
    }

    /**
     * @author alex.meng
     * @description 清除所有用户缓存（慎用）
     **/
    public void clearAllUserCache() {
        long count = redisHelper.removeByPattern(CacheConstants.Patterns.USER_ALL);
        log.warn("清空所有用户缓存: count={}", count);
    }
}

